package com.nmgsr.nettools.base;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.nmgsr.nettools.R;
import com.nmgsr.nettools.util.LogUtil;
import com.trello.rxlifecycle2.components.support.RxFragment;

//import butterknife.ButterKnife;

/**
 * Created by vincent on 2016/1/6.
 */
public abstract class BaseFragment extends RxFragment {
    protected View rootView;
    protected View view_status_bar;
    protected TextView tv_right, tv_center, tv_left;
    protected ImageView iv_status_bar, iv_left, iv_right;
    protected RelativeLayout rl_left, rl_right, fl_actionbar;
    protected FrameLayout fl_content;

//    protected LoadService mBaseLoadService;

    public Context mContext;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        findView();
        initView();
        initData();
        LogUtil.e("----->onCreateView");
        return rootView;
    }

    private void findView() {
        LogUtil.setTag(getClass().getSimpleName());
        mContext = getActivity();

        rootView = View.inflate(mContext, R.layout.root_view, null);
        iv_status_bar = rootView.findViewById(R.id.iv_status_bar);
        view_status_bar = rootView.findViewById(R.id.view_status_bar);
        tv_left = rootView.findViewById(R.id.tv_left);
        tv_center = rootView.findViewById(R.id.tv_center);
        tv_right = rootView.findViewById(R.id.tv_right);
        iv_left = rootView.findViewById(R.id.iv_left);
        iv_right = rootView.findViewById(R.id.iv_right);
        rl_left = rootView.findViewById(R.id.rl_left);
        rl_right = rootView.findViewById(R.id.rl_right);
        fl_actionbar = rootView.findViewById(R.id.fl_actionbar);
        fl_content = rootView.findViewById(R.id.fl_content);

        tv_center.setText(getActionBarTitle() == null ? "" : getActionBarTitle());
        rl_left.setOnClickListener(v -> ((Activity) mContext).finish());

        fl_actionbar.setVisibility(View.GONE);

        View content = View.inflate(mContext, getLayoutId(), null);
        if (content != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            fl_content.addView(content, params);
//            mBaseLoadService = LoadSir.getDefault().register(content, (Callback.OnReloadListener) this::onNetReload);
        }
//        ButterKnife.bind(this, rootView);
    }

    protected abstract int getLayoutId();

    public abstract void initView();

    protected String getActionBarTitle() {
        return null;
    }

    protected void initData() {
    }

}
